//
//  STDiscountsViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/11.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGChoseDisCountViewController.h"
#import "TMGDiscountsTableViewCell.h"
@interface TMGChoseDisCountViewController ()

@end

@implementation TMGChoseDisCountViewController

#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    NSString * title = @"优惠券";
    self.title = title;
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:[NSString stringWithFormat:@"暂无%@",title] buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];

    
    
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self st_setLeftItemWithTitle:@"取消" titleColor:[UIColor whiteColor]];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TMGDiscountsTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    TMGDiscountsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[TMGDiscountsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    __weak typeof(self) weakSelf =  self;
    TMGDiscountModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [formatter setTimeZone:GTMzone];
    NSDate * date = [formatter dateFromString:model.end];
    NSInteger num = [date timeIntervalSince1970];
    NSInteger now = [[NSDate st_localCurrentDate] timeIntervalSince1970];
    if (num >= now) {
        [cell.useButton setTitle:@"立即领取" forState:UIControlStateNormal];
        [cell.useButton setTitleColor:TM_orangeColor forState:UIControlStateNormal] ;
        cell.useButton.userInteractionEnabled = YES;
        cell.useButton.hidden = YES;
        
        
    }else{
        cell.useButton.hidden = NO;
        [cell.useButton setTitle:@"已失效" forState:UIControlStateNormal];
        [cell.useButton setTitleColor:ThirdTextColor forState:UIControlStateNormal] ;
        cell.useButton.userInteractionEnabled = NO;
    }
    [cell.useButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedUseButtonWithIndexPath:indexPath];
    }];
    if ([model.title containsString:@"加息"]) {
        cell.useButton.hidden = YES;
        cell.titleLable.text = [NSString stringWithFormat:@"%@(%@元可以使用)",cell.titleLable.text,model.more];
    }
    if ([model.title containsString:@"新手"]) {
        cell.useButton.hidden = YES;
        cell.titleLable.text = [NSString stringWithFormat:@"%@(%@元代金券)",cell.titleLable.text,
                                model.number];
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TMGDiscountModel * model = self.dataSouce[indexPath.row];
    if (self.chosedHandle) {
         [self.navigationController popViewControllerAnimated:YES];
        self.chosedHandle(model);
       
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)st_leftBarAction:(id)sender{
    if (self.chosedHandle) {
        [self.navigationController popViewControllerAnimated:YES];
        self.chosedHandle(nil);
    }
}
- (void)onSelectedUseButtonWithIndexPath:(NSIndexPath*)indexPath{
    TMGDiscountModel * model = self.dataSouce[indexPath.row];
   // [self sendUseRequestWithID:model.discountID.description];
}

@end

